package com.example.actionsignin.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.actionsignin.R;
import com.example.actionsignin.bean.ImageHolder;

import java.util.List;
import java.util.Map;

import static android.content.ContentValues.TAG;

/**
 * Created by 旭 on 2017/7/31/031.
 */

public class AddImageAdapter extends BaseAdapter {

    private Context mContext;
    private List<Map<String,Object>> mList;
    private LayoutInflater mLayoutInflater;

    public AddImageAdapter(Context context, List<Map<String,Object>> list)
    {
        this.mContext = context;
        this.mList = list;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View viewItem;
        ImageHolder imageHolder = null;
        if (convertView == null)
        {
            imageHolder = new ImageHolder();
            viewItem = mLayoutInflater.inflate(R.layout.grid_item_add_image,parent,false);
            imageHolder.imageView = (ImageView) viewItem.findViewById(R.id.grid_item_describe_image);

            viewItem.setTag(imageHolder);

            convertView = viewItem;
        }else {
            imageHolder = (ImageHolder) convertView.getTag();
        }

        if (mList.size() > 0)
        {
            boolean bitmap = false;
            String path = "";
            if (mList.get(position).get("bitmap") != null)
            {
                bitmap = (boolean) mList.get(position).get("bitmap");
            }

            if (mList.get(position).get("path") != null)
            {
                path = String.valueOf(mList.get(position).get("path"));
            }

            if (bitmap)
            {
                Glide.with(mContext).load(R.mipmap.camera).into(imageHolder.imageView);
            }else if (path != null && !path.equals("")){
                Glide.with(mContext).load(path).into(imageHolder.imageView);
            }
        }

        return convertView;
    }
}
